package com.qf.quduba.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.wxpay.sdk.WXPay;
import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.config.MyWxPayConfig;
import com.qf.quduba.entity.Books;
import com.qf.quduba.entity.ModulVO;
import com.qf.quduba.entity.Recharge;
import com.qf.quduba.service.RechargeService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 17312
 * @time2020/10/1315:01
 */
@RestController
@CrossOrigin
public class RechargeController {
    @Resource
    private RechargeService rechargeService;
    @PostMapping("/add")
    public ResultVO addRecharge(@RequestBody Recharge recharge) throws Exception {
        String rechargeId = rechargeService.addRecharge(recharge);

        WXPay wxPay = new WXPay(new MyWxPayConfig());
        Map<String,String> data = new HashMap<>();
        data.put("body", "趣币充值");
        data.put("out_trade_no",rechargeId);
        data.put("device_info", "");
        //支付单位：人民币
        data.put("fee_type", "CNY");
        //订单支付金额 单位：分
        //data.put("total_fee", (int)(order.getOrderPrice()*100)+"");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        //付款状态的回调接口
        data.put("notify_url", "http://jun.free.idcfengye.com/pay/callback");
        // 此处指定为扫码支付
        data.put("trade_type", "NATIVE");
        data.put("product_id","");

        Map<String, String> respMap = wxPay.unifiedOrder(data);
        String payUrl = respMap.get("code_url");


        return new ResultVO(1000,rechargeId,payUrl);
    }

    @PostMapping("/deleteRecharges")
    public ResultVO deleteRecharges(@RequestBody ModulVO modulVO){
        int i = rechargeService.deleteRecharges(modulVO);
        if(i > 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }
    @PostMapping("/deleteRecharge")
    public ResultVO deleteRecharge(@RequestParam String rechargeId){
        int i = rechargeService.deleteRecharge(rechargeId);
        if(i > 0){
            return new ResultVO(1000,"成功",null);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("/rechargeList")
    public Map<String, Object> findUserRecharge(@RequestParam(value = "page", defaultValue = "1", required = false)Integer page,@RequestParam(value = "limit", defaultValue = "10", required = false)Integer limit,String userId){
        PageHelper.startPage(page, limit); // 使用PageHelper插件实现分页效果
        List<Recharge> userRechargeList = rechargeService.findUserRecharge(userId);
        PageInfo pageInfo = new PageInfo(userRechargeList); // 使用PageInfo对象封装查询结果
        Map<String, Object> map = new HashMap<>();
        map.put("data", pageInfo.getList()); // 得到当前页的数据
        map.put("code", 0);
        map.put("count", pageInfo.getTotal()); // 得到总条数
        return map;
    }
}
